package com.jboltai.pptextract;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.util.IOUtils;

import java.awt.Rectangle;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.nio.file.Path;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

/**
 * Utility class to generate a sample PPT presentation for testing
 */
public class SamplePresentationGenerator {

    /**
     * Generate a sample PPT presentation
     *
     * @param outputPath Path to save the presentation
     * @throws IOException If an I/O error occurs
     */
    public static void generateSamplePresentation(Path outputPath) throws IOException {
        try (XMLSlideShow ppt = new XMLSlideShow()) {
            // Slide 1
            XSLFSlide slide1 = ppt.createSlide();

            // Title
            XSLFTextShape title1 = slide1.createTextBox();
            title1.setAnchor(new Rectangle(50, 50, 500, 50));
            title1.setText("JBoltAI");
            title1.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24.0);

            // Content
            XSLFTextShape content1 = slide1.createTextBox();
            content1.setAnchor(new Rectangle(50, 120, 600, 300));
            content1.setText("JBoltAI是Java企业级全栈AI数智化应用极速开发框架，\n\n旨在为Java技术团队构筑AI应用开发能力。\n\n帮助Java系统快速接入大模型能力并开发具有AI能力的功能模块。\n\n提供包含多大模型适配接入（国内外）、AI知识库(RAG)、思维链(Event)、\n\nFunction Calling、MCP、Agent智能体开发工具箱、AI流程编排等数十项支撑能力。\n\nJBoltAI具备国内领先的用AI如何改造系统的\n\nAIGS(Artificial Intelligence Generated Service)解决方案。");

            // Slide 2
            XSLFSlide slide2 = ppt.createSlide();

            // Title
            XSLFTextShape title2 = slide2.createTextBox();
            title2.setAnchor(new Rectangle(50, 50, 500, 50));
            title2.setText("官方网址：");
            title2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24.0);

            // Bullet points
            XSLFTextShape bulletPoints = slide2.createTextBox();
            bulletPoints.setAnchor(new Rectangle(50, 120, 600, 300));

            XSLFTextParagraph p1 = bulletPoints.addNewTextParagraph();
            p1.setBullet(true);
            XSLFTextRun r1 = p1.addNewTextRun();
            r1.setText("https://jboltai.com");

            XSLFTextParagraph p2 = bulletPoints.addNewTextParagraph();
            p2.setBullet(true);
            XSLFTextRun r2 = p2.addNewTextRun();
            r2.setText("现在分两个版本");

            XSLFTextParagraph p3 = bulletPoints.addNewTextParagraph();
            p3.setBullet(true);
            XSLFTextRun r3 = p3.addNewTextRun();
            r3.setText("JBoltAI_SpringBoot基座版和JBoltAI_JFinal基座版");

            // Slide 3
            XSLFSlide slide3 = ppt.createSlide();

            // Title
            XSLFTextShape title3 = slide3.createTextBox();
            title3.setAnchor(new Rectangle(50, 50, 500, 50));
            title3.setText("详情请到官网了解");
            title3.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24.0);

            // Formatted text
            XSLFTextShape formattedText = slide3.createTextBox();
            formattedText.setAnchor(new Rectangle(50, 120, 600, 300));

            XSLFTextParagraph fp1 = formattedText.addNewTextParagraph();
            XSLFTextRun fr1 = fp1.addNewTextRun();
            fr1.setText("This text is bold. ");
            fr1.setBold(true);

            XSLFTextRun fr2 = fp1.addNewTextRun();
            fr2.setText("This text is italic. ");
            fr2.setItalic(true);

            XSLFTextRun fr3 = fp1.addNewTextRun();
            fr3.setText("This text is underlined.");
            fr3.setUnderlined(true);

            XSLFTextParagraph fp2 = formattedText.addNewTextParagraph();
            XSLFTextRun fr4 = fp2.addNewTextRun();
            fr4.setText("This is a new paragraph with normal text.");

            // Slide 4 - With images
            XSLFSlide slide4 = ppt.createSlide();

            // Title
            XSLFTextShape title4 = slide4.createTextBox();
            title4.setAnchor(new Rectangle(50, 50, 500, 50));
            title4.setText("Image Example");
            title4.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24.0);

            // Add description
            XSLFTextShape imageDesc = slide4.createTextBox();
            imageDesc.setAnchor(new Rectangle(50, 120, 600, 50));
            imageDesc.setText("This slide contains a generated image for testing media extraction:");

            // Create a simple image
            BufferedImage img = createSampleImage(300, 200);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img, "png", baos);
            byte[] imageBytes = baos.toByteArray();

            // Add the image to the slide
            XSLFPictureData pictureData = ppt.addPicture(
                    new ByteArrayInputStream(imageBytes),
                    XSLFPictureData.PictureType.PNG);
            XSLFPictureShape picture = slide4.createPicture(pictureData);
            picture.setAnchor(new Rectangle(50, 180, 300, 200));

            // Save the presentation
            try (FileOutputStream out = new FileOutputStream(outputPath.toFile())) {
                ppt.write(out);
            }
        }
    }

    /**
     * Create a simple sample image for testing
     *
     * @param width Image width
     * @param height Image height
     * @return The generated image
     */
    private static BufferedImage createSampleImage(int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // Create a simple gradient background
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int red = (x * 255) / width;
                int green = (y * 255) / height;
                int blue = 128;
                int color = (red << 16) | (green << 8) | blue;
                img.setRGB(x, y, color);
            }
        }

        // Draw a simple shape
        java.awt.Graphics2D g = img.createGraphics();
        g.setColor(Color.WHITE);
        g.fillOval(width/4, height/4, width/2, height/2);
        g.setColor(Color.BLACK);
        g.drawOval(width/4, height/4, width/2, height/2);
        g.dispose();

        return img;
    }

    public static void main(String[] args) {
        try {
            Path outputPath = Path.of("src/test/resources/jboltai.pptx");
            generateSamplePresentation(outputPath);
            System.out.println("JBoltAI PPT created at: " + outputPath);
        } catch (IOException e) {
            System.err.println("Error creating JBoltAI PPT: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
